package com.yitong.equipment.ui

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.gyf.immersionbar.ImmersionBar
import com.yitong.equipment.R
import com.yitong.equipment.base.BaseActivity
import com.yitong.equipment.databinding.ActivityMainBinding
import com.yitong.equipment.ui.find.FindFragment
import com.yitong.equipment.ui.home.HomeFragment
import com.yitong.equipment.ui.mine.MineFragment
import com.yitong.equipment.ui.mall.MallFragment
import com.yitong.equipment.ui.viewmodel.MainViewModel
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.LogUtils
import com.gyf.immersionbar.OnKeyboardListener


class MainTabActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), View.OnClickListener {

    lateinit var mFragments: ArrayList<Fragment>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        setListener()
    }

    private fun initView() {
        mFragments = ArrayList()
        var homeFragment = HomeFragment()
        var mallFragment = MallFragment()
        var findFragment = FindFragment()
        var mineFragment = MineFragment()
        mFragments.add(homeFragment)
        mFragments.add(mallFragment)
        mFragments.add(findFragment)
        mFragments.add(mineFragment)
        bindingView.viewPager2.adapter = MyAdapter(this, mFragments)
        bindingView.viewPager2.offscreenPageLimit = 2
        bindingView.viewPager2.isUserInputEnabled = false
        bindingView.viewPager2.orientation = ViewPager2.ORIENTATION_HORIZONTAL
        bindingView.mainTab.llHome.isSelected = true
    }


    private fun setListener() {
        bindingView.mainTab.llHome.setOnClickListener(this)
        bindingView.mainTab.llMall.setOnClickListener(this)
        bindingView.mainTab.llFind.setOnClickListener(this)
        bindingView.mainTab.llMine.setOnClickListener(this)

        bindingView.viewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                when (position) {
                    0 -> {
                        tabSelected(bindingView.mainTab.llHome)
                        ImmersionBar.with(this@MainTabActivity).keyboardEnable(true).setOnKeyboardListener { isPopup, keyboardHeight ->
                            LogUtils.dTag("isPopup", isPopup)
                            if (isPopup) {
                                bindingView.mainTab.ll.visibility = View.GONE
                            } else {
                                bindingView.mainTab.ll.visibility = View.VISIBLE
                            }
                        }.statusBarDarkFont(false).init()
                    }
                    1 -> {
                        tabSelected(bindingView.mainTab.llMall)
                        ImmersionBar.with(this@MainTabActivity).keyboardEnable(false).statusBarDarkFont(false).init()
                    }
                    2 -> {
                        tabSelected(bindingView.mainTab.llFind)
                        ImmersionBar.with(this@MainTabActivity).keyboardEnable(false).statusBarDarkFont(false).init()
                    }
                    3 -> {
                        tabSelected(bindingView.mainTab.llMine)
                        ImmersionBar.with(this@MainTabActivity).keyboardEnable(false).statusBarDarkFont(false).init()
                    }
                }
            }
        })
    }


    inner class MyAdapter(fragmentActivity: FragmentActivity, private var fragments: List<Fragment>) : FragmentStateAdapter(fragmentActivity) {
        override fun getItemCount(): Int {
            return fragments.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }
    }

    override fun onClick(v: View?) {

        when (v?.id) {
            R.id.ll_home -> {
                bindingView.viewPager2.setCurrentItem(0, false)
                tabSelected(bindingView.mainTab.llHome)
            }

            R.id.ll_mall -> {
                bindingView.viewPager2.setCurrentItem(1, false)
                tabSelected(bindingView.mainTab.llMall)
            }
            R.id.ll_find -> {
                bindingView.viewPager2.setCurrentItem(2, false)
                tabSelected(bindingView.mainTab.llFind)
            }
            R.id.ll_mine -> {
                bindingView.viewPager2.setCurrentItem(3, false)
                tabSelected(bindingView.mainTab.llMine)
            }

        }
    }

    private fun tabSelected(linearLayout: LinearLayout) {
        bindingView.mainTab.llHome.isSelected = false
        bindingView.mainTab.llMall.isSelected = false
        bindingView.mainTab.llFind.isSelected = false
        bindingView.mainTab.llMine.isSelected = false
        linearLayout.isSelected = true
    }


    override fun initImmersionBar() {
        ImmersionBar.with(this).init()
    }

    override fun isRegisterEventBus(): Boolean {
        return true
    }

    override fun isImmersionBarEnabled(): Boolean {
        return true
    }


}